<?php $this->load->view('templates/backend/header'); ?>
<h3><?php echo $this->lang->line("voucher_list");?></h3>

<style>
.num_cls{
    width:15%;
}
.btn-success,.btn-danger{
    vertical-align: top;
}
.notice{
    color:green;
}
</style>

<?php if (isset($flash_message)  && $flash_message!=NULL ): ?>
<div class="alert alert-success"><a class="close" customerAssess-dismiss="alert">x</a><strong><?php echo $flash_message; ?></strong></div>
<?php endif; ?>
<div class="well">
    <?php echo form_open('/auth/voucher/index/add');?>
        <div class="tab-content">	
            <div class="tab-pane active" id="main">
                <?php
                //thông tin khách hàng
                echo form_label($this->lang->line('phone'), 'phone');
                echo form_input(array('name' => 'phone', 'id' => 'phone', 'value' => ($voucher->phone)?$voucher->phone:'','required'=>'required','class'=>'num_cls','placeholder'=>$this->lang->line('phone'),'class'=>'num_cls'));
                echo form_error('phone');

                echo form_input(array('type'=>'hidden', 'name' => 'customerID', 'id' => 'customerID', 'value' => ($voucher->customerID)?$voucher->customerID:''));
                //action add
                echo '<div id="foneadd">';//begin id foneadd
                echo form_label($this->lang->line('voucher_info'), 'booking_types');
                foreach($voucher->booking_info as $keys => $vals){
                    $keys += 1;
                    echo '<div id="fone_addon'.$keys.'" class="fonediv">';
                    //Loại booking
                    echo form_input(array('type'=>'hidden', 'name' => 'booking_types[]', 'id' => "booking_types".$keys, 'value' => ($vals['booking_types'])?$vals['booking_types']:''));
                    echo form_dropdown('booking_types[]', $this->voucher_model->getListBookingTypes(), $vals['booking_types'],'id="booking_types'.$keys.'" class="num_cls" disabled="disabled"');
                    echo form_error('booking_types');
                    echo ' ';
                    //hãng
                    echo form_input(array('readonly' => 'true','name' => 'airline[]', 'id' => "airline".$keys, 'value' => $vals['airline'],'required'=>'required','maxlength'=>'2','class'=>'num_cls','placeholder'=>$this->lang->line('airline')));
                    echo form_error('airline');
                    echo ' ';
                    //code
                    echo form_input(array('readonly' => 'true','name' => 'booking_place_code[]', 'id' => "booking_place_code".$keys, 'value' => $vals['booking_place_code'],'required'=>'required','class'=>'num_cls','placeholder'=>$this->lang->line('booking_place_code')));
                    echo form_error('booking_place_code');
                    echo ' ';
                    //ngay dat
                    echo form_input(array('type' => 'hidden','name' => 'createdTime[]', 'id' => "createdTime".$keys, 'value' => $vals['createdTime']));
                    //Giá net
                    echo form_input(array('name' => 'net_fare[]', 'id' => "net_fare".$keys, 'value' => $vals['net_fare'].' VNĐ','readonly' => 'true','required'=>'required','class'=>'num_cls','placeholder'=>$this->lang->line('net_fare')));
                    echo form_error('net_fare');
                    echo form_input(array('type' => 'hidden','name' => 'net_fare_fake[]', 'id' => "net_fare_fake".$keys, 'value' => $vals['net_fare_fake'],'class'=>'sumNet'));
                    echo ' ';
                    //Giá bán
                    echo form_input(array('name' => 'sale_fare[]', 'id' => "sale_fare".$keys, 'value' => $vals['sale_fare'].' VNĐ','readonly' => 'true','required'=>'required','class'=>'num_cls','placeholder'=>$this->lang->line('sale_fare')));
                    echo form_error('sale_fare');
                    echo form_input(array('type' => 'hidden','name' => 'sale_fare_fake[]', 'id' => "sale_fare_fake".$keys, 'value' => $vals['sale_fare_fake'],'class'=>'sale_fare'));
                    echo ' ';
                    //echo form_button(array('adata' => $keys,'content' => $this->lang->line('add'),'class' => 'btn btn-success'));
                    //echo '<span id="simple-msg-phone'.$keys.'"></span>';
                    echo '</div>';
                }
                echo '</div>';//end id foneadd
                
                echo form_input(array('type' => 'hidden','name' => 'payment_method_fake', 'id' => 'payment_method_fake', 'value' => ($voucher->payment_method)?$voucher->payment_method:''));
                
                // Tong net
                echo form_label($this->lang->line('sumNet'), 'sumNet');
                echo form_input(array('name' => 'sumNet', 'id' => 'sumNet', 'readonly' => 'true','value' => ($voucher->sumNet)?$voucher->sumNet:'','placeholder'=>$this->lang->line('sumNet')));
                echo form_input(array('type' => 'hidden','name' => 'sumNet_fake', 'id' => 'sumNet_fake', 'value' => ($voucher->sumNet)?$voucher->sumNet:''));
                echo form_error('sumNet');
                 // Tong gia ban
                echo form_label($this->lang->line('sumSale'), 'sumSale');
                echo form_input(array('name' => 'sumSale', 'id' => 'sumSale', 'readonly' => 'true','value' => ($voucher->sumSale)?$voucher->sumSale:'','placeholder'=>$this->lang->line('sumSale')));
                echo form_input(array('type' => 'hidden','name' => 'sumSale_fake', 'id' => 'sumSale_fake', 'value' => ($voucher->sumSale)?$voucher->sumSale:''));
                echo form_error('sumSale');
                //Phương thức thanh toán
                echo form_label($this->lang->line('payment_method').' <span class="required">*</span>', 'payment_method');
                echo form_dropdown('payment_method', $this->voucher_model->getListPaymentMethod(), ($voucher->payment_method)?$voucher->payment_method:'','id="payment_method"');
                echo form_error('payment_method');
                echo '<span id="payment_method_form"></span>';
                //Ghi chú
                echo form_label($this->lang->line('note_bk'), 'note_bk');
                echo form_textarea(array('name' => 'note_bk', 'id' => 'note_bk', 'value' => ($voucher->note_bk)?$voucher->note_bk:'','rows'=>'5'));
                echo form_error('note_bk');
                ?>
            </div>
        </div>
        <div class="top-button">
                <?php echo form_button(array('id' => 'submit', 'value' => 'Update', 'name' => 'submit', 'type' => 'submit', 'content' => $this->lang->line('save'),'class' => 'btn btn-primary')); ?>
                <?php echo form_button(array('id' => 'cancel','content' => $this->lang->line('close'),'class' => 'btn','onclick'=>'closeFancy()')); ?>
        </div>
    <?php form_close();?>
    <span id="simple-msg-submit"></span>
</div>

<?php $this->load->view('templates/backend/footer'); ?>
<?php $this->load->view('templates/backend/lib'); ?>

<script src="<?php echo base_url().'static/templates/backend/js/autoNumeric.js';?>"></script>
<script type="text/javascript">
    function closeFancy(){
        parent.jQuery.fancybox.close();
    }
    $(document).ready(function() {
            var url_segment = '<?php echo $this->uri->segment(4);?>';
            $('button[type="submit"]').attr('disabled','disabled');
            $("#phone").attr('readonly','true');
            $('#sumNet').autoNumeric('init',{aSign:' VNĐ',mDec:0, pSign:'s' });
            $('#sumSale').autoNumeric('init',{aSign:' VNĐ',mDec:0, pSign:'s' });
            
            var input_add = <?php echo ($voucher->length)?$voucher->length+1:'0';?>;
            $("button.btn-success").on('click',function (e) {
                setTimeout(function () {
                    $.ajax({
                        url : '/auth/voucher/getInfoAirCode',
                        type: "POST",
                        data : {add: input_add},
                        cache: true,
                        success:function(data) 
                        {
                            $("#foneadd").append(data);
                            input_add = input_add + 1;
                        },
                        error: function() 
                        {
                            alert("Có lỗi xảy ra khi thêm dữ liệu! Liên hệ Admin!");return false;
                        }
                    });
                }, 111);
            });
            
            $( "#sumNet" ).on('click',function() {
                    //sumNet
                    var sumNet = 0;
                    $(".sumNet").each(function(){
                        sumNet = Number($(this).val()) + Number(sumNet);
                    });
                    document.getElementById("sumNet").value             = FormatNumber(sumNet);
                    document.getElementById("sumNet_fake").value        = sumNet;
                    $('#payment_method').removeAttr('disabled');
            });
            
            $( "#sumSale" ).on('click',function() {
                    //sumSale
                    var sumSale = 0;
                    $(".sumSale").each(function(){
                        sumSale = Number($(this).val()) + Number(sumSale);
                    });
                    document.getElementById("sumSale").value            = FormatNumber(sumSale);
                    document.getElementById("sumSale_fake").value       = sumSale;
                    $('#payment_method').removeAttr('disabled');
            });
            
            $( "#payment_method" ).change(function() {
                $("#payment_method_form").show();
                if($(this).val() == ""){
                    alert("Vui lòng chọn Phương thức thanh toán");$("#payment_method_form").hide();$('button[type="submit"]').attr('disabled','disabled');return false;
                }
                if(url_segment == 'add'){
                    $("#note_bk").removeAttr('readonly');
                    $("#note_bk").focus();
                }
                $('button[type="submit"]').removeAttr('disabled');
                updateURL = '/auth/booking/paymentMethod?id='+$(this).val()+'&cid='+$("#customerID").val();
                $.ajax(
                {
                        url : updateURL,
                        success:function(data)
                        {
                           $("#payment_method_form").html(data);
                        },
                        error: function() 
                        {
                            $("#simple-msg").html('Vui lòng liên hệ Admin!');
                        }
                });
            });
            
            $('form').submit(function() {
                    if($("#payment_method").val() == null || $("#payment_method").val() == ''){
                        alert("Vui lòng chọn <?php echo $this->lang->line('payment_method')?>");
                        return false;
                    } else {
                        $(this).find("button[type='submit']").prop('disabled',true);
                    }
            });
    });
    
    function FormatNumber(x) {
        if (typeof x === "undefined") {
            return '';
        } else {
        return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") + ' VNĐ';
        }
    };
    ///,|VNĐ|\s/g
    function UnFormatNumber(x) {
        if (typeof x === "undefined") {
            return '';
        } else {
        return x.toString().replace(/,|VNĐ|\s/g, "");
        }
    };
</script>